capture log close
log using uscsr20-pew18, replace text

//  program:    uscsr20-pew18.do
//  task:       Analysis 
//  project:    US Public Opinion on Cross-Strait Relations
//  author:     Hsin-Hsin Pan
//  date:       June/1/2020

// #1
// program setup

   version 16
   clear all 
   set linesize 80
  
/////////////////////////////////////////
// # 
// Load data 

clear all

use us_csr_analysisp_jjps.dta, clear

/////////////////////////////////////////
// Figure 1
// US public opinion on the China-Taiwan tension

graph bar csr4_1_100 csr4_2_100 csr4_3_100 csr4_4_100 [aweight=weight], ///
	  bargap(10) ///
      blabel(bar,color(black) position(outside) format(%9.1f) size(small))  ///
      scheme(s1color)  ///
	  bar(1, bcolor(gs1*0.2)) ///
      bar(2, bcolor(gs1*0.4)) ///
      bar(3, bcolor(gs1*0.6)) ///
      bar(4, bcolor(gs1*0.8)) ///
      ylabel(0(10)50, labsize(small))  ///
      ytitle("% of Respondents", size(small)) ///
      legend(order(1 "Not a Problem" ///
			       2 "Not Too Serious" ///
		           3 "Somewhat Serious" ///
				   4 "Very Serious") ///
      position(6) row(1) size(small) symysize(2) keygap(.5) symxsize(2) forcesize) 

gr export fig1.png, replace 

/////////////////////////////////////////
// Fig 2
// China threat and the China-Taiwan tension
// Level of China Threat

graph bar csr2_2_100 [aweight=weight], over(threat_ch3) stack ///
          scheme(s1color) ///
          blabel(bar, position(outside) format(%9.1f) size(small))  ///
          bar(1, bcolor(black)) ///
	      ylabel(0(20)100, labsize(small)) bargap(20) legend(off) /// 
		  ytitle("% of Respondents Considering China-Taiwan Tension a Serious Problem to the US", size(small)) ///
          text(-5.8 50 "Level of China Threat", size(small)) saving(threat_lvl.gph, replace)
	  
// Dimension of China Threat

graph bar csr2_2_100 [aweight=weight], over(threat3) stack ///
          scheme(s1color) ///
          blabel(bar, position(outside) format(%9.1f) size(small))  ///
          bar(1, bcolor(black)) ///
	      ylabel(0(20)100, labsize(small)) ///
	      ytitle("% of Respondents Considering China-Taiwan Tension a Serious Problem to the US", size(small)) ///
          text(-5.8 50 "Issue of China Threat", size(small)) saving(threat_dim.gph, replace)

gr combine threat_lvl.gph threat_dim.gph

gr export fig2.png, replace

////////////////////////////////////////
// Table 1
// Ordered logit models for the perception of the China-Taiwan tension
// m1

logit csr2 threat_ch3  ///
	       age6 male edu2 i.pty3 polid5 ///
	       [pw=weight], robust
eststo m1

// odds ratio 

logit csr2 threat_ch3  ///
	       age6 male edu2 i.pty3 polid5 ///
	       [pw=weight], or robust

////////////////////////////////////////
// m2

logit csr2 i.threat3  /// 
	       age6 male edu2 i.pty3 polid5  /// 
	       [pw=weight], robust
eststo m2	

// odds ratio 

logit csr2 i.threat3  /// 
	       age6 male edu2 i.pty3 polid5  /// 
	       [pw=weight], or robust

////////////////////////////////////////
// m3

logit csr2 threat_ch3 i.threat3 /// 
	       age6 male edu2 i.pty3 polid5  /// 
	       [pw=weight], robust
eststo m3

// odds ratio 

logit csr2 threat_ch3 i.threat3 /// 
	       age6 male edu2 i.pty3 polid5  /// 
	       [pw=weight], or robust

////////////////////////////////////////
// m4

logit csr2 threat_ch3 i.threat3 /// 
	       age6 male edu2 i.pty3 polid5  /// 
		   ch_job2 ch_deficit2 /// 
	       ch_rights2 ch_cyber2 /// 
	       [pw=weight], robust
eststo m4

// odds ratio 

logit csr2 threat_ch3 i.threat3 /// 
	       age6 male edu2 i.pty3 polid5  /// 
		   ch_job2 ch_deficit2 /// 
	       ch_rights2 ch_cyber2 /// 
	       [pw=weight], or robust
  
esttab m1 m2 m3 m4 using "tab1.csv", replace label nodepvar nonumber ///
	b(3) se(3) ar2 scalar(ll)    ///
	sfmt(%12.3f) brackets ///
    star(* 0.05 ** 0.01 *** 0.001) 

////////////////////////////////////////
// Fig 3	
// Predicted probabilities and the differentials for the China-Taiwan tension
// being a serious problem for the US
// Not A Threat
// Economic Strength

set seed 168
 
estsimp logit csr2 threat_ch3 threat_mil threat_both /// level + dimension
	       age6 male edu2 democrat independent polid5  /// demographics
		   ch_job2 ch_deficit2 /// economic
	       ch_rights2 ch_cyber2 /// political
	       [pw=weight], robust
		   
setx (threat_ch3) 0 /// Not A Threat
     (threat_mil threat_both) 0 /// Economic Strength
	 (democrat independent) 0 /// Republican 
     (age6 male edu2 polid5 ch_job2 ch_deficit2 ch_rights2 ch_cyber2) median 
	 
simqi 

//////////////////
// Not A Threat
// Military Strength

drop b1-b14
	
estsimp logit csr2 threat_ch3 threat_mil threat_both /// level + dimension
	       age6 male edu2 democrat independent polid5  /// demographics
		   ch_job2 ch_deficit2 /// economic
	       ch_rights2 ch_cyber2 /// political
	       [pw=weight], robust
		   
setx (threat_ch3) 0 /// Not A Threat
     (threat_mil) 1 (threat_both) 0 /// Military Strength
	 (democrat independent) 0 /// Republican 
     (age6 male edu2 polid5 ch_job2 ch_deficit2 ch_rights2 ch_cyber2) median 
	 
simqi 

//////////////////
// Not A Threat
// Both

drop b1-b14
	
estsimp logit csr2 threat_ch3 threat_mil threat_both /// level + dimension
	       age6 male edu2 democrat independent polid5  /// demographics
		   ch_job2 ch_deficit2 /// economic
	       ch_rights2 ch_cyber2 /// political
	       [pw=weight], robust
		   
setx (threat_ch3) 0 /// Not A Threat
     (threat_mil) 0 (threat_both) 1 /// Both
	 (democrat independent) 0 /// Republican 
     (age6 male edu2 polid5 ch_job2 ch_deficit2 ch_rights2 ch_cyber2) median 
	 
simqi 

//////////////////
// Minor Threat
// Economic Strength

drop b1-b14
 
estsimp logit csr2 threat_ch3 threat_mil threat_both /// level + dimension
	       age6 male edu2 democrat independent polid5  /// demographics
		   ch_job2 ch_deficit2 /// economic
	       ch_rights2 ch_cyber2 /// political
	       [pw=weight], robust
		   
setx (threat_ch3) 1 /// Minor Threat
     (threat_mil threat_both) 0 /// Economic Strength
	 (democrat independent) 0 /// Republican 
     (age6 male edu2 polid5 ch_job2 ch_deficit2 ch_rights2 ch_cyber2) median 
	 
simqi 

//////////////////
// Minor Threat
// Militry Strength

drop b1-b14

estsimp logit csr2 threat_ch3 threat_mil threat_both /// level + dimension
	       age6 male edu2 democrat independent polid5  /// demographics
		   ch_job2 ch_deficit2 /// economic
	       ch_rights2 ch_cyber2 /// political
	       [pw=weight], robust
		   
setx (threat_ch3) 1 /// Minor Threat
     (threat_mil) 1 (threat_both) 0 /// Military Strength
	 (democrat independent) 0 /// Republican 
     (age6 male edu2 polid5 ch_job2 ch_deficit2 ch_rights2 ch_cyber2) median 
	 
simqi

//////////////////
// Minor Threat
// Both

drop b1-b14
 
estsimp logit csr2 threat_ch3 threat_mil threat_both /// level + dimension
	       age6 male edu2 democrat independent polid5  /// demographics
		   ch_job2 ch_deficit2 /// economic
	       ch_rights2 ch_cyber2 /// political
	       [pw=weight], robust
		   
setx (threat_ch3) 1 /// Minor Threat
     (threat_mil) 0 (threat_both) 1 /// Both
	 (democrat independent) 0 /// Republican 
     (age6 male edu2 polid5 ch_job2 ch_deficit2 ch_rights2 ch_cyber2) median 
	 
simqi

//////////////////
// Major Threat
// Economic Strength

drop b1-b14
 
estsimp logit csr2 threat_ch3 threat_mil threat_both /// level + dimension
	       age6 male edu2 democrat independent polid5  /// demographics
		   ch_job2 ch_deficit2 /// economic
	       ch_rights2 ch_cyber2 /// political
	       [pw=weight], robust
		   
setx (threat_ch3) 2 /// Major Threat
     (threat_mil threat_both) 0 /// Economic Strength
	 (democrat independent) 0 /// Republican 
     (age6 male edu2 polid5 ch_job2 ch_deficit2 ch_rights2 ch_cyber2) median 
	 
simqi 

//////////////////
// Major Threat
// Military Strength

drop b1-b14
 
estsimp logit csr2 threat_ch3 threat_mil threat_both /// level + dimension
	       age6 male edu2 democrat independent polid5  /// demographics
		   ch_job2 ch_deficit2 /// economic
	       ch_rights2 ch_cyber2 /// political
	       [pw=weight], robust
		   
setx (threat_ch3) 2 /// Major Threat
     (threat_mil) 1 (threat_both) 0 /// Military Strength
	 (democrat independent) 0 /// Republican 
     (age6 male edu2 polid5 ch_job2 ch_deficit2 ch_rights2 ch_cyber2) median 
	 
simqi 

//////////////////
// Major Threat
// Both

drop b1-b14
 
estsimp logit csr2 threat_ch3 threat_mil threat_both /// level + dimension
	       age6 male edu2 democrat independent polid5  /// demographics
		   ch_job2 ch_deficit2 /// economic
	       ch_rights2 ch_cyber2 /// political
	       [pw=weight], robust
		   
setx (threat_ch3) 2 /// Major Threat
     (threat_mil) 0 (threat_both) 1 /// Both
	 (democrat independent) 0 /// Republican 
     (age6 male edu2 polid5 ch_job2 ch_deficit2 ch_rights2 ch_cyber2) median 
	 
simqi 

//////////////////
// difference of predicted probability
// Baseline: Economic Strength

drop b1-b14

set seed 168

estsimp logit csr2 threat_ch3 threat_mil threat_both /// level + dimension
	       age6 male edu2 democrat independent polid5  /// demographics
		   ch_job2 ch_deficit2 /// economic
	       ch_rights2 ch_cyber2 /// political
	       [aw=weight], robust

//////////////////		   
// Not A Threat
// Military Strength 

setx (threat_ch3) 0 /// 
     (threat_both) 0 /// 
 	 (democrat independent) 0 /// 
     (age6 male edu2 polid5 ch_job2 ch_deficit2 ch_rights2 ch_cyber2) median ///
	 
simqi, fd(prval(1)) changex(threat_mil 0 1)		

//////////////////
// Not A Threat
// Both

setx (threat_ch3) 0 /// 
     (threat_mil) 0 /// 
 	 (democrat independent) 0 ///  
     (age6 male edu2 polid5 ch_job2 ch_deficit2 ch_rights2 ch_cyber2) median ///
	 
simqi, fd(prval(1)) changex(threat_both 0 1)		

//////////////////		   
// Minor Threat
// Military Strength 

setx (threat_ch3) 1 /// 
     (threat_both) 0 /// 
 	 (democrat independent) 0 /// 
     (age6 male edu2 polid5 ch_job2 ch_deficit2 ch_rights2 ch_cyber2) median ///
	 
simqi, fd(prval(1)) changex(threat_mil 0 1)	

//////////////////
// Minor Threat
// Both

setx (threat_ch3) 1 /// 
     (threat_mil) 0 /// 
 	 (democrat independent) 0 ///  
     (age6 male edu2 polid5 ch_job2 ch_deficit2 ch_rights2 ch_cyber2) median ///
	 
simqi, fd(prval(1)) changex(threat_both 0 1) 
		   
//////////////////		   
// Major Threat
// Military Strength 

setx (threat_ch3) 2 /// 
     (threat_both) 0 /// 
 	 (democrat independent) 0 /// 
     (age6 male edu2 polid5 ch_job2 ch_deficit2 ch_rights2 ch_cyber2) median ///
	 
simqi, fd(prval(1)) changex(threat_mil 0 1)	

//////////////////
// Minor Threat
// Both

setx (threat_ch3) 2 /// 
     (threat_mil) 0 /// 
 	 (democrat independent) 0 ///  
     (age6 male edu2 polid5 ch_job2 ch_deficit2 ch_rights2 ch_cyber2) median ///
	 
simqi, fd(prval(1)) changex(threat_both 0 1) 

/////////////////////////////////////////////

clear all
use "pp_jjps.dta", clear

twoway scatter mean level3 if dimension3 == 1, mcolor(black) || ///
       rcap up low  level3 if dimension3 == 1, lcolor(black) || ///
	   scatter mean level3 if dimension3 == 2, mcolor(black) || ///
       rcap up low  level3 if dimension3 == 2, lcolor(black) || ///
	   scatter mean level3 if dimension3 == 3, mcolor(black) || ///
       rcap up low  level3 if dimension3 == 3, lcolor(black) ///
	   scheme(plotplain) saving(pp.gph, replace) ///
	   legend(position(6) row(1) ///
	   order(1 "Economic Strength" 3 "Military Strength" 5 "Both")) ///
	   ytitle("Predicted Probability of China-Taiwan Tension Being a Serious Problem to the US", size(small)) ///
	   xtitle("Level of China Threat", size(small)) ///
	   ylabel(0.5(0.10)1) ///
	   xlabel(0.75(0.25)3.25) ///
	   xscale(range(0.7 3.2)) 
	   
// difference of predicted probability

clear all
use "dpp_jjps.dta", clear
	   
twoway scatter mean level3 if dimension3 == 1, mcolor(black) || ///
       rcap up low  level3 if dimension3 == 1, lcolor(black) || ///
	   scatter mean level3 if dimension3 == 2, mcolor(black) || ///
       rcap up low  level3 if dimension3 == 2, lcolor(black)  ///
	   scheme(plotplain) saving(dpp.gph, replace) ///
	   legend(position(6) row(1) ///
	   order(1 "Military Strength" 3 "Both")) ///
	   ytitle("Differentials of Predicted Probability of China-Taiwan Tension Being a Serious Problem to the US", size(small)) ///
	   xtitle("Level of China Threat", size(small)) ///
	   ylabel(-0.2(0.05)0.2) ///
	   xlabel(0.75(0.25)3.25) yline(0, lpattern(dash) lcolor(red)) ///
	   xscale(range(0.7 3.2))

gr combine pp.gph dpp.gph

gr export fig3.gnp, replace	  

///////////////////////////////
// Appendix I
// summary statistics

su csr2 csr4 threat_ch3 threat3 ///
   ch_job2 ch_deficit2 ch_rights2 ch_cyber2 /// 
   age6 male edu2 pty3 polid5 
   
///////////////////////////////
// Appendix II
// Ordered logit models for the perception of the China-Taiwan tension	
// m5

ologit csr4 threat_ch3  /// 
	       age6 male edu2 i.pty3 polid5  /// 
	       [pw=weight], robust
eststo m5

// odds ratio 

ologit csr4 threat_ch3  /// level
	       age6 male edu2 i.pty3 polid5  /// 
	       [pw=weight], or robust

////////////////////////////////////////
// m6

ologit csr4 i.threat3  /// 
	       age6 male edu2 i.pty3 polid5  /// 
	       [pw=weight], robust
eststo m6	

// odds ratio 

ologit csr4 i.threat3  /// 
	       age6 male edu2 i.pty3 polid5  /// 
	       [pw=weight], or robust

////////////////////////////////////////
// m7
// level + dimension + demographics

ologit csr4 threat_ch3 i.threat3 /// 
	       age6 male edu2 i.pty3 polid5  /// 
	       [pw=weight], robust
eststo m7

// odds ratio 

ologit csr4 threat_ch3 i.threat3 /// 
	       age6 male edu2 i.pty3 polid5  /// 
	       [pw=weight], or robust

////////////////////////////////////////
// m8

ologit csr4 threat_ch3 i.threat3 /// 
	       age6 male edu2 i.pty3 polid5  /// 
		   ch_job2 ch_deficit2 /// 
	       ch_rights2 ch_cyber2 /// 
	       [pw=weight], robust
eststo m8
		   
// odds ratio 

ologit csr4 threat_ch3 i.threat3 /// 
	       age6 male edu2 i.pty3 polid5  /// 
		   ch_job2 ch_deficit2 /// 
	       ch_rights2 ch_cyber2 /// 
	       [pw=weight], or robust
  

esttab m5 m6 m7 m8 using "app2.csv", replace label nodepvar nonumber ///
	b(3) se(3) ar2 scalar(ll)    ///
	sfmt(%12.3f) brackets ///
    star(* 0.05 ** 0.01 *** 0.001) 

//////////////////////////////////////////////////////////////////// 	 
// # 
// Close log file & exit do-file
    
    log close
    exit 	